import ajax from "./module/ajax.js";
import { renderImgList, NewSwiper } from "./module/tools.js";

let swiper = document.querySelector("#swiper");
let wrapper = document.querySelector("#swiper .swiper-wrapper");

let photo = document.querySelectorAll(".photo");

let tijianBtn = document.querySelectorAll(".menuImg");

let codeImg = document.querySelectorAll(".codeImg");

let map = document.querySelectorAll(".map");

let token = null;
const API = 'http://website.bluej.cn';

window.onload = async function () {
    //  登陆请求获取token
    try {
        let res = await ajax({
            url: API + '/api/admin/login',
            method: 'POST',
            data: {
                phone: '13302408576',
                password: '123456'
            }
        });
        token = res.data.token;

    } catch (error) {
        console.log(error);
    }

    // banner动态获取轮播图
    try {
        let getCarousel = await ajax({
            url: API + '/api/carouselGroup/get',
            data: {
                carouselGroupId: 339
            },
            headers: {
                'x-token': token
            }
        })
        console.log(getCarousel);
        let carouselList = getCarousel.data.Carousels;
        //传入需要添加轮播图组的节点和轮播图的url数组
        renderImgList(wrapper, carouselList);
        //创建swiper
        var mySwiper = NewSwiper(swiper, { speed: 1500 });
    } catch (error) {
        console.log(error);
    }

    // “现在体检”等按钮动态获取图片
    try {
        let getBtnImg = await ajax({
            url: API + '/api/carouselGroup/get',
            headers: {
                "x-token": token
            },
            data: {
                carouselGroupId: 341
            }
        })
        let btnImgList = getBtnImg.data.Carousels;
        for (let i = 0; i < btnImgList.length; i++) {
            let Img = new Image();
            Img.src = btnImgList[i].url;
            tijianBtn[i].appendChild(Img)
        }
    } catch (error) {
        console.log(error);
    }

    // “套餐推荐”动态获取照片
    try {
        let getPhoto = await ajax({
            url: API + '/api/carouselGroup/get',
            headers: {
                "x-token": token
            },
            data: {
                carouselGroupId: 340
            }
        })
        let photoList = getPhoto.data.Carousels;
        for (let i = 0; i < photoList.length; i++) {
            let Img = new Image();
            Img.src = photoList[i].url;
            photo[i].appendChild(Img)
        }
    } catch (error) {
        console.log(error);
    }

    // 动态获取地图
    try {
        let getMapImg = await ajax({
            url: API + '/api/carouselGroup/get',
            headers: {
                "x-token": token
            },
            data: {
                carouselGroupId: 342
            }
        })
        let mapList = getMapImg.data.Carousels;
        for (let i = 0; i < mapList.length; i++) {
            let Img = new Image();
            Img.src = mapList[i].url;
            map[i].appendChild(Img)
        }
    } catch (error) {
        console.log(error);
    }

    // 动态获取二维码图片
    try {
        let getCodeImg = await ajax({
            url: API + '/api/carouselGroup/get',
            headers: {
                "x-token": token
            },
            data: {
                carouselGroupId: 343
            }
        })
        let codeImgList = getCodeImg.data.Carousels;
        for (let i = 0; i < codeImgList.length; i++) {
            let Img = new Image();
            Img.src = codeImgList[i].url;
            codeImg[i].appendChild(Img)
        }
    } catch (error) {
        console.log(error);
    }
}

